package com.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾讯云对象存储
 */
@Configuration
public class CosClientConfig {
    @Value("${jujiaoyuan.cos.secret-id}")
    private String secretId;
    @Value("${jujiaoyuan.cos.secret-key}")
    private String secretKey;
    @Value("${jujiaoyuan.cos.region}")
    private String region;

    @Bean
    public COSClient cosClient() {

        // 1 传入获取到的临时密钥 (tmpSecretId, tmpSecretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分
        //COS_REGION 参数：配置成存储桶 bucket 的实际地域，例如 ap-beijing，更多 COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端
        return new COSClient(cred, clientConfig);
    }

}
